package com.qiwo.bpm.controller;


import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import io.swagger.models.properties.StringProperty;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qiwo.common.utils.ReflectUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qiwo.common.base.AdminBaseController;
import com.qiwo.bpm.domain.FlowType;
import com.qiwo.bpm.service.FlowTypeService;
import com.qiwo.common.utils.Result;

/**
 * 
 * <pre>
 * 流程类型
 *  @author  chenwenchao
 *  @date 2021-04-01 16:37:07
 * </pre>
 * <small> 2021-04-01 16:37:07 | chenwenchao</small>
 */
@Controller
@RequestMapping("/bpm/type")
public class TypeController extends AdminBaseController {
	@Autowired
	private FlowTypeService flowTypeService;
	
	@GetMapping()
	@RequiresPermissions("bpm:type:type")
	String Type(){
	    return "bpm/type/type";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("bpm:type:type")
	public Result<Page<FlowType>> list(String sortName, String sortOrder, FlowType typeDTO){
        Wrapper<FlowType> wrapper = new EntityWrapper<FlowType>();
        if(StringUtils.isNotBlank(sortName)){
            StringProperty fieldStr = new StringProperty();
            fieldStr.setDefault(sortName);
            List<Map<String,String>> mapList= ReflectUtils.getColumnName(FlowType.class);
            Optional<Map<String,String>> optionalStringStringMap=mapList.stream().
                    filter(f->f.get("fieldNames").equals(fieldStr.getDefault())).findFirst();
            if(optionalStringStringMap.isPresent()){
                sortName=optionalStringStringMap.get().get("tableField");
                boolean isAsc=false;
                if(sortOrder.equals("asc")){
                    isAsc=true;
                }
                wrapper.orderBy(sortName,isAsc);
            }else{
                wrapper.orderBy("id",false);
            }

        }

        Page<FlowType> page = flowTypeService.selectPage(getPage(FlowType.class), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("bpm:type:add")
	String add(){
	    return "bpm/type/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("bpm:type:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		FlowType type = flowTypeService.selectById(id);
		model.addAttribute("type", type);
	    return "bpm/type/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("bpm:type:add")
	public Result<String> save( FlowType type){
		flowTypeService.insert(type);
        return Result.ok();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("bpm:type:edit")
	public Result<String>  update( FlowType type){
		boolean update = flowTypeService.updateById(type);
		return update ? Result.ok() : Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("bpm:type:remove")
	public Result<String>  remove( Integer id){
		flowTypeService.deleteById(id);
        return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("bpm:type:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] ids){
		flowTypeService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
